Title: SQL BT (By-BKK) / 19 Subqueries
Tags: Obsidian_to_Anki

Q: What is a subquery in SQL?
A: A query nested inside another query

Q: Where can you use subqueries in SQL?
A: Inside SELECT, FROM, WHERE, or HAVING clauses

Q: True/False: Subqueries must be enclosed in parentheses
A: True

Q: Give an example of a subquery being used in a WHERE clause
A: SELECT * FROM sales_associates WHERE salary > (SELECT AVG(revenue_generated) FROM sales_associates);

Q: What is a correlated subquery?
A: A subquery that uses columns from the outer query

Q: How is a correlated subquery different from a standard subquery?
A: It runs for each row in the outer query and depends on its data

Q: Give an example of a correlated subquery
A: SELECT * FROM employees WHERE salary > (SELECT AVG(revenue_generated) FROM employees AS dept_employees WHERE dept_employees.department = employees.department);

Q: Why should you use meaningful aliases in subqueries?
A: To make complex queries easier to read and understand

Q: What performance consideration is there with correlated subqueries?
A: They can be slow and hard to optimize

Q: What does the IN operator do with subqueries?
A: Tests if a column value appears in the results of a subquery

Q: How does a subquery work with the IN operator?
A: The subquery provides a dynamic list of values to compare

Q: How does IN use subqueries in SQL?
A: The result of a subquery in IN can be used as a list of allowed values

Q: What makes a correlated subquery different from a normal subquery?
A: Correlated subqueries depend on outer query values and are run for each row

Q: True/False: Subqueries in SQL do not need parentheses
A: Subqueries must be wrapped in parentheses

Q: Why should you use aliases with subqueries?
A: Aliases make complex subqueries easier to read

Q: What is a subquery in SQL?
A: A query placed inside another query